package com.commodity.Dao.warehouseDaoImp;

import com.commodity.Dao.WarehouseDao;
import com.commodity.common.Global;
import com.commodity.entity.Warehouse;
import com.commodity.entity.WarehouseSearchBean;
import com.commodity.utils.PaginateInfo;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

public class WareHouseDaoImp implements WarehouseDao {
    private final JdbcTemplate jdbcTemplate = new JdbcTemplate(Global.getDataSource());
    private final RowMapper<Warehouse> MAPPER = new BeanPropertyRowMapper<>(Warehouse.class);

    @Override
    public List<Warehouse> findAll(WarehouseSearchBean wsb, PaginateInfo pi) {
        String countSql = "select count(0) from t_warehouse ";
        StringBuilder sb = new StringBuilder();
        List<Object> args = new ArrayList<>();
        if (StringUtils.hasText(wsb.getWarehouseId())) {
            sb.append(" and warehouse_id = ?");
            args.add(wsb.getWarehouseId());
        }
        if (StringUtils.hasText(wsb.getWarehouseName())) {
            sb.append(" and warehouse_name = ?");
            args.add(wsb.getWarehouseName());
        }
        if (StringUtils.hasText(wsb.getAddress())) {
            sb.append(" and address like ?");
            args.add("%" + wsb.getAddress() + "%");
        }
        if (wsb.getAreaFrom() != null) {
            sb.append(" and area >= ?");
            args.add(wsb.getAreaFrom());
        }
        if (wsb.getAreaTo() != null) {
            sb.append(" and area < ?");
            args.add(wsb.getAreaTo());
        }
        if (StringUtils.hasText(wsb.getPhone())) {
            sb.append(" and phone like ?");
            args.add("%" + wsb.getPhone() + "%");
        }
        countSql += " where 1=1 " + sb.toString();
        Integer count = jdbcTemplate.queryForObject(countSql, Integer.class, args.toArray());
        pi.setTotal(count);

        //查询学生记录
        String sql = "select id,warehouse_id,warehouse_name,address,area,phone from t_warehouse ";

        sql += " where 1=1 " + sb.toString() + " limit ?,?";
        args.add(pi.getOffset());
        args.add(pi.getLimit());
        return jdbcTemplate.query(sql, MAPPER, args.toArray());
    }

    @Override
    public int add(Warehouse warehouse) {
        String sql = "insert into t_warehouse (warehouse_id,warehouse_name,address,area,phone) values(?,?,?,?,?)";

        return jdbcTemplate.update(sql, warehouse.getWarehouseId(), warehouse.getWarehouseName(), warehouse.getAddress(), warehouse.getArea(), warehouse.getPhone());
    }

    @Override
    public int deleteByIds(Integer[] intIds) {
        String sql = "delete from t_warehouse where id in(";
        for (int i = 0; i < intIds.length; i++) {
            if (i == intIds.length - 1) {
                sql += "?";
            } else {
                sql += "?,";
            }
        }
        sql += ")";
        return jdbcTemplate.update(sql, intIds);
    }

    @Override
    public Warehouse findById(Integer id) {
        String sql = "select id,warehouse_id,warehouse_name,address,area,phone from t_warehouse where id = ?";
        List<Warehouse> warehouseList = jdbcTemplate.query(sql,MAPPER,id);
        return warehouseList.isEmpty()?null:warehouseList.get(0);
    }

    @Override
    public int update(Warehouse warehouse) {
        String sql = "update t_warehouse set warehouse_id = ?,warehouse_name = ?,address = ?,area = ?,phone = ? where id = ?";
        return jdbcTemplate.update(sql,warehouse.getWarehouseId(),warehouse.getWarehouseName(),warehouse.getAddress(), warehouse.getArea(),warehouse.getPhone(),warehouse.getId());

    }
}
